
package com.base.app_core.View;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.base.app_core.R;
import com.base.app_core.base.impl.BaseActivity;
import com.base.app_core.base.impl.BasePresenter;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.log.F;

import java.util.List;

/**
 * Created by xiangheng.song on 2017/3/1.
 */

public abstract class AbstractMainActivity<T extends BasePresenter> extends BaseActivity<T> {

    protected TabFragmentHost tabHost;

    private List<Class<?>> fragmentList;
    private List<Integer> fragmentIconList;
    private List<String> fragmentTitleList;
    private AlphaAnimation animation;
    private boolean isAniFinishFirst = true;

    protected abstract View getWelComeView();

    View welcomeView = null;


    public abstract void onWelcomeFinish();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpWelcomViewAni();
        setup();
        initTabView();
    }

    private void setUpWelcomViewAni() {
        if (animation == null) {
            animation = new AlphaAnimation(1.0f, 1.0f);
            welcomeView = getWelComeView();
            animation.setDuration(AppConstants.WELCOME_DELAY);
            if (null != welcomeView) {
                welcomeView.setAnimation(animation);
            }
            animation.start();
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    welcomeView.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    welcomeView.setVisibility(View.GONE);  //隐藏起来，不需要任何布局空间
                    onWelcomeFinish();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }
    }

    public void stopWelcomeViewAni() {
        //同样cancel()也能取消掉动画
        if (null != welcomeView)
            if (animation != null && isAniFinishFirst) {
                new Handler(getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        welcomeView.clearAnimation();
                        animation.cancel();
                        isAniFinishFirst = false;
                    }
                }, 1000);
            }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected abstract List<Class<?>> getFragmentList();

    protected abstract List<Integer> getFragmentIconList();

    protected abstract List<String> getFragmentTitleList();

    private void setup() {
        tabHost = (TabFragmentHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        fragmentList = getFragmentList();
        fragmentIconList = getFragmentIconList();
        fragmentTitleList = getFragmentTitleList();
    }

    private void initTabView() {
        Bundle bundle = new Bundle();
        for (int i = 0; i < fragmentList.size(); i++) {
            View indicator = getIndicatorView(i);
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(fragmentTitleList.get(i)).setIndicator(indicator);
            tabHost.addTab(tabSpec, fragmentList.get(i), bundle);

        }
        tabHost.getTabWidget().setDividerDrawable(getResources().getDrawable(R.color.transparent));
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                F.j(tabId);

            }
        });
    }


    private View getIndicatorView(int i) {
        View view = getLayoutInflater().inflate(R.layout.main_tab_item, null);
        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.cartcontainer);
        TextView title = (TextView) view.findViewById(R.id.tab_title);
        title.setText(fragmentTitleList.get(i));
        ImageView icon = (ImageView) view.findViewById(R.id.tab_icon);
//        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
//        if (i == 0) {
//            linearLayout.setPadding(0, 0, XYBAUtils.dip2px(16), 0);
//        } else if (i == 1) {
//            linearLayout.setPadding(XYBAUtils.dip2px(16), 0, 0, 0);
//        }
        icon.setImageResource(fragmentIconList.get(i));
        return view;
    }
}
